package com.nmsl.controller;

import com.nmsl.domain.GyfDoctorSchedule;
import com.nmsl.domain.GyfRegistrationManagement;
import com.nmsl.domain.ScheduleTimeDistribution;
import com.nmsl.mapper.GyfDoctorScheduleMapper;
import com.nmsl.mapper.GyfRegistrationManagementMapper;
import com.nmsl.mapper.ScheduleTimeDistributionMapper;
import com.nmsl.util.R.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@RequestMapping("/std")
@RestController
public class gyf_ScheduleTimeDistributionController {
    @Autowired
    private ScheduleTimeDistributionMapper scheduleTimeDistributionMapper;
    @Autowired
    private GyfRegistrationManagementMapper gyfRegistrationManagementMapper;
    @Autowired
    private GyfDoctorScheduleMapper gyfDoctorScheduleMapper;
//    @RequestMapping("/insert")
//    public void insert() {
//        scheduleTimeDistributionMapper.insert();
//    }



    //通过排班时间编号和时间段来减少挂票号数量-1
    @RequestMapping("/reduceMount")

    public R reduceMount(int id,String date){
        System.out.println("打印一下传来的id和date:"+id+"----"+date);
        //首先先判断这个时间段对应的字段是哪个
        String time = date.substring(0, 5);
        ScheduleTimeDistribution STD = new ScheduleTimeDistribution();
        STD.setScheduleTimeId(id);
        System.out.println("打印一下time:");
        System.out.println(time);

        if(time.equals("09:00")){
            STD.setUpWorkTime1(new Date());
        }else if(time.equals("09:30")){
            STD.setUpWorkTime2(new Date());
        }else if(time.equals("10:00")){
            STD.setUpWorkTime3(new Date());
        }else if(time.equals("10:30")){
            STD.setUpWorkTime4(new Date());
        }else if(time.equals("11:00")){
            STD.setUpWorkTime5(new Date());
        }else if(time.equals("11:30")){
            STD.setUpWorkTime6(new Date());
        }else if(time.equals("13:30")){
            STD.setDoWorkTime1(new Date());
        }else if(time.equals("14:00")){
            STD.setDoWorkTime2(new Date());
        }else if(time.equals("14:30")){
            STD.setDoWorkTime3(new Date());
        }else if(time.equals("15:00")){
            STD.setDoWorkTime4(new Date());
        }else if(time.equals("15:30")){
            STD.setDoWorkTime5(new Date());
        }else if(time.equals("16:00")){
            STD.setDoWorkTime6(new Date());
        }else if(time.equals("16:30")){
            STD.setDoWorkTime7(new Date());
        }else if(time.equals("17:00")){
            STD.setDoWorkTime8(new Date());
        }else if(time.equals("17:30")){
            STD.setDoWorkTime9(new Date());
        }

        System.out.println("打印一下传过去的STD:"+STD);
        int i = scheduleTimeDistributionMapper.updateByIdAndTime(STD);
        if(i == 1){
            return R.ok(null);
        }else{
            return R.fail("操作失败");
        }

    }



    //通过排班时间编号和时间段来减少挂票号数量-1
    @RequestMapping("/reduceMount2")

    public R reduceMount2(@RequestBody String json){
        System.out.println("打印参数1111");
        //首先先判断这个时间段对应的字段是哪个
//        String time = date.substring(0, 5);
//        ScheduleTimeDistribution STD = new ScheduleTimeDistribution();
//        STD.setScheduleTimeId(id);
//        System.out.println("打印一下time:");
//        System.out.println(time);
//
//        if(time.equals("09:00")){
//            STD.setUpWorkTime1(new Date());
//        }else if(time.equals("09:30")){
//            STD.setUpWorkTime2(new Date());
//        }else if(time.equals("10:00")){
//            STD.setUpWorkTime3(new Date());
//        }else if(time.equals("10:30")){
//            STD.setUpWorkTime4(new Date());
//        }else if(time.equals("11:00")){
//            STD.setUpWorkTime5(new Date());
//        }else if(time.equals("11:30")){
//            STD.setUpWorkTime6(new Date());
//        }else if(time.equals("13:30")){
//            STD.setDoWorkTime1(new Date());
//        }else if(time.equals("14:00")){
//            STD.setDoWorkTime2(new Date());
//        }else if(time.equals("14:30")){
//            STD.setDoWorkTime3(new Date());
//        }else if(time.equals("15:00")){
//            STD.setDoWorkTime4(new Date());
//        }else if(time.equals("15:30")){
//            STD.setDoWorkTime5(new Date());
//        }else if(time.equals("16:00")){
//            STD.setDoWorkTime6(new Date());
//        }else if(time.equals("16:30")){
//            STD.setDoWorkTime7(new Date());
//        }else if(time.equals("17:00")){
//            STD.setDoWorkTime8(new Date());
//        }else if(time.equals("17:30")){
//            STD.setDoWorkTime9(new Date());
//        }
//
//        System.out.println("打印一下传过去的STD:"+STD);
//        int i = scheduleTimeDistributionMapper.updateByIdAndTime(STD);
//        if(i == 1){
//            return R.ok(null);
//        }else{
//            return R.fail("操作失败");
//        }
    return R.ok(null);
    }


    //通过patientid  取消挂号  然后挂号数量+1
    @RequestMapping("/addMount")
    public R addMount(int patientid){

        //首先通过patientid查询这个患者的挂号记录
        GyfRegistrationManagement grm = gyfRegistrationManagementMapper.selectByPatientId(patientid);

        //首先要先删除挂号记录
        gyfRegistrationManagementMapper.deleteByPrimaryKey(grm.getRegistrationId());



        //拿到记录后开始获得医生的id 挂号日期  和挂号时间段
        Integer doctorId = grm.getDoctorId();
        Date registrationTime2 = grm.getRegistrationTime();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        String format = sdf.format(registrationTime2);
        String registrationTime=format;


        Date registrationDate = grm.getRegistrationDate();


        //医生的id和 挂号日期去查到  排班时间编号
        GyfDoctorSchedule gyfDoctorSchedule = gyfDoctorScheduleMapper.selectByDoctorIdAndDate(doctorId, registrationDate);

        //然后获取排班时间编号
        Integer scheduleTimeId = gyfDoctorSchedule.getScheduleTimeDistribution().getScheduleTimeId();

        //通过排班 时间编号 和 挂号时间段 来给这个时间段+1
                ScheduleTimeDistribution STD = new ScheduleTimeDistribution();
                STD.setScheduleTimeId(scheduleTimeId);
                if(registrationTime.equals("09:00")){
                    STD.setUpWorkTime1(new Date());
                }else if(registrationTime.equals("09:30")){
                    STD.setUpWorkTime2(new Date());
                }else if(registrationTime.equals("10:00")){
                    STD.setUpWorkTime3(new Date());
                }else if(registrationTime.equals("10:30")){
                    STD.setUpWorkTime4(new Date());
                }else if(registrationTime.equals("11:00")){
                    STD.setUpWorkTime5(new Date());
                }else if(registrationTime.equals("11:30")){
                    STD.setUpWorkTime6(new Date());
                }else if(registrationTime.equals("13:30")){
                    STD.setDoWorkTime1(new Date());
                }else if(registrationTime.equals("14:00")){
                    STD.setDoWorkTime2(new Date());
                }else if(registrationTime.equals("14:30")){
                    STD.setDoWorkTime3(new Date());
                }else if(registrationTime.equals("15:00")){
                    STD.setDoWorkTime4(new Date());
                }else if(registrationTime.equals("15:30")){
                    STD.setDoWorkTime5(new Date());
                }else if(registrationTime.equals("16:00")){
                    STD.setDoWorkTime6(new Date());
                }else if(registrationTime.equals("16:30")){
                    STD.setDoWorkTime7(new Date());
                }else if(registrationTime.equals("17:00")){
                    STD.setDoWorkTime8(new Date());
                }else if(registrationTime.equals("17:30")){
                    STD.setDoWorkTime9(new Date());
                }

        System.out.println("打印一下准备传过去的参数");
        System.out.println("STD:"+STD);
        System.out.println("打印一下时间片段:"+format);
        int i = scheduleTimeDistributionMapper.updateByIdAndTime2(STD);
                if(i == 1){
                    return R.ok(null);
                }else{
                    return R.fail("操作失败");
                }
    }



    //通过 医生id  日期 和时间 来判断是否有票
    @GetMapping("/isHasCount")
    public R isHasCount(int DoctorId,String date,String time){
        System.out.println("首先打印一下确认票数的参数");
        System.out.println(DoctorId);
        System.out.println(date);
        System.out.println(time);


        //首先通过医生id和日期 来获得排班时间编号
        GyfDoctorSchedule gyfDoctorSchedule = gyfDoctorScheduleMapper.selectByDoctorIdAndDate(DoctorId,java.sql.Date.valueOf(date));

        //然后开始取得排班时间编号
        Integer scheduleTimeId = gyfDoctorSchedule.getScheduleTimeDistribution().getScheduleTimeId();


        //通过排班时间编号  查询排班时间  信息
        ScheduleTimeDistribution scheduleTimeDistribution = scheduleTimeDistributionMapper.selectByPrimaryKey(scheduleTimeId);

        //判断time处于哪个时间段
        int cout =0;
        if(time.equals("09:00")){
            cout=scheduleTimeDistribution.getUpRemainingReservations1();
        }else if(time.equals("09:30")){
            cout=scheduleTimeDistribution.getUpRemainingReservations2();
        }else if(time.equals("10:00")){
            cout=scheduleTimeDistribution.getUpRemainingReservations3();
        }else if(time.equals("10:30")){
            cout=scheduleTimeDistribution.getUpRemainingReservations4();
        }else if(time.equals("11:00")){
            cout=scheduleTimeDistribution.getUpRemainingReservations5();
        }else if(time.equals("11:30")){
            cout=scheduleTimeDistribution.getUpRemainingReservations6();
        }else if(time.equals("13:30")){
            cout=scheduleTimeDistribution.getDoRemainingReservations1();
        }else if(time.equals("14:00")){
            cout=scheduleTimeDistribution.getDoRemainingReservations2();
        }else if(time.equals("14:30")){
            cout=scheduleTimeDistribution.getDoRemainingReservations3();
        }else if(time.equals("15:00")){
            cout=scheduleTimeDistribution.getDoRemainingReservations4();
        }else if(time.equals("15:30")){
            cout=scheduleTimeDistribution.getDoRemainingReservations5();
        }else if(time.equals("16:00")){
            cout=scheduleTimeDistribution.getDoRemainingReservations6();
        }else if(time.equals("16:30")){
            cout=scheduleTimeDistribution.getDoRemainingReservations7();
        }else if(time.equals("17:00")){
            cout=scheduleTimeDistribution.getDoRemainingReservations8();
        }else if(time.equals("17:30")){
            cout=scheduleTimeDistribution.getDoRemainingReservations9();
        }

        if(cout>0){
            return R.ok(cout);
        }else{
            return R.fail("此时间段已无票");
        }

    }


//    查询现在可分配的排班时间id
    @GetMapping("/selectUseable")
    public R selectUseable(){
        List<Integer> scheduleTimeDistributions = scheduleTimeDistributionMapper.selectUseableId();
        if(scheduleTimeDistributions.size()>0){
            System.out.println("打印一下所有的排班时间");
            System.out.println(scheduleTimeDistributions);
            return R.ok(scheduleTimeDistributions);
        }else{
            return R.fail("现在没有可分配的排班时间");
        }
    }

    //  插入一条可分配的排班时间id
    @PostMapping("/insert")
    public R insert(){
        int insert = scheduleTimeDistributionMapper.insert();
        if(insert == 1){
            return R.ok(null);
        }else{
            return R.fail("插入失败");
        }
    }

    //通过排班时间id删除排班时间记录
    @DeleteMapping("/deleteByid/{id}")
    public R deleteByid(@PathVariable Integer id){
        int i = scheduleTimeDistributionMapper.deleteByPrimaryKey(id);
        if(i == 1){
            return R.ok(null);
        }else{
            return R.ok("删除失败");
        }
    }
}
